package com.liu.project.service.impl;

import com.liu.project.service.VerifyService;
import com.liu.project.utils.RedisUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * @program: project
 * @description: 验证码
 * @author: 86187
 * @create: 2022-02-03 15:35
 **/

@Service
public class VerifyServiceImpl implements VerifyService {

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    RedisUtil redisUtil;

    @Value("${spring.mail.username}")
    String from;


        @Override
        public void sendVerifyCode(String email) {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

            simpleMailMessage.setSubject("[xx网站] 您的注册验证码");

            Random random = new Random();
            int code = random.nextInt(899999)+100000;
            redisUtil.set("Verify:Code"+email,code+"",3*60);
            simpleMailMessage.setText("您的注册验证码为："+code+"，三分钟内有效，请及时完成注册！如果不是本人操作，请忽略。");

            simpleMailMessage.setTo(email);
            simpleMailMessage.setFrom(from);
            System.out.println(from);

            javaMailSender.send(simpleMailMessage);


        }

//    检验验证码是否一致
        @Override
        public boolean doVerity(String email, String code) {
            Object o = redisUtil.get("Verify:Code" + email);
            System.out.println("验证码:"+o);
            if (o==null) return false;
            return o.equals(code);
        }

}
